/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Feb 12, 2009, 10:57:26 PM
 */
package magis;

import edu.byu.intex2.data.Model;
import edu.byu.intex2.data.dao.EmployeeDAO;
import edu.byu.intex2.tester.TestDataLayerTests;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JFrame;

/**
 *
 * @author Brady
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
        initComponents();

        center(this);

        // Test our DAOs
        try {
            testDAOs();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Test the DAOs */
    private void testDAOs() throws Exception {
        TestDataLayerTests tests = new TestDataLayerTests();
        //tests.testPerson();
        //tests.testEmployee();
        //tests.testCustomer();
        //tests.testSalesperson();
        //tests.testPhysicalCar();
        //tests.testUsedCar();
        //tests.testNewCar();
        //tests.testWarranty();
        //tests.testSale();
        //tests.testSaleLine();
        //tests.testTradeInLine();
        //tests.testSaleWithBothLines();
    }

    /** Center the JFrame */
    public static void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();
        Rectangle bounds = ge.getMaximumWindowBounds();
        int w = frame.getWidth();
        int h = frame.getHeight();
        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        customerGroup = new javax.swing.JPanel();
        username_lbl = new javax.swing.JLabel();
        login_btn = new javax.swing.JButton();
        password_lbl = new javax.swing.JLabel();
        username_txt = new javax.swing.JTextField();
        error_lbl = new javax.swing.JLabel();
        password_txt = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Madison Automotive Group");

        customerGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee Login"));
        customerGroup.setName("customerGroup"); // NOI18N

        username_lbl.setFont(new java.awt.Font("Tahoma", 0, 10));
        username_lbl.setText("Username:");

        login_btn.setFont(new java.awt.Font("Tahoma", 0, 8));
        login_btn.setText("Login");
        login_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                login_btnActionPerformed(evt);
            }
        });

        password_lbl.setFont(new java.awt.Font("Tahoma", 0, 10));
        password_lbl.setText("Password:");

        username_txt.setText("dusting");

        error_lbl.setFont(new java.awt.Font("Tahoma", 0, 10));
        error_lbl.setForeground(new java.awt.Color(153, 0, 0));
        error_lbl.setText("Please login.");

        password_txt.setText("mypassword");
        password_txt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                password_txtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout customerGroupLayout = new javax.swing.GroupLayout(customerGroup);
        customerGroup.setLayout(customerGroupLayout);
        customerGroupLayout.setHorizontalGroup(
            customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerGroupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(error_lbl, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .addGroup(customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, customerGroupLayout.createSequentialGroup()
                            .addComponent(password_lbl)
                            .addGap(18, 18, 18)
                            .addComponent(password_txt))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(login_btn)
                            .addGroup(customerGroupLayout.createSequentialGroup()
                                .addComponent(username_lbl)
                                .addGap(18, 18, 18)
                                .addComponent(username_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        customerGroupLayout.setVerticalGroup(
            customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerGroupLayout.createSequentialGroup()
                .addGroup(customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(username_lbl)
                    .addComponent(username_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(customerGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password_lbl)
                    .addComponent(password_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(login_btn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(error_lbl))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customerGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customerGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void login_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_login_btnActionPerformed
        // Login Employee and set employee to model
        try {
            Model.getInstance().employee = EmployeeDAO.getInstance().login(username_txt.getText(), password_txt.getText());
//            System.out.println(Model.getInstance().employee.getFirstName());
            error_lbl.setText("Welcome, " + Model.getInstance().employee.getFullName());
        } catch (Exception e) {
            error_lbl.setText(e.getMessage());
//            e.printStackTrace();
        }
    }//GEN-LAST:event_login_btnActionPerformed

    private void password_txtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_password_txtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_password_txtActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel customerGroup;
    private javax.swing.JLabel error_lbl;
    private javax.swing.JButton login_btn;
    private javax.swing.JLabel password_lbl;
    private javax.swing.JPasswordField password_txt;
    private javax.swing.JLabel username_lbl;
    private javax.swing.JTextField username_txt;
    // End of variables declaration//GEN-END:variables
}
